/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import entities.Action;
import entities.Month;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import utils.ChocoDate;

/**
 *
 * @author chocoseb
 */
public class ActionsTableModel extends AbstractTableModel {

    private String[] _columnsNames = {
       "Categorie", "Date", "Description", "Montant"
    };
    private ArrayList<Action> _listActions = new ArrayList<Action>();

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    @Override
    public String getColumnName(int p_col) {
        String result;
        if (p_col < this.getColumnCount()) {
            result = _columnsNames[p_col];
        } else {
            result = "";
        }
        return result;
    }

    public int getColumnCount() {
        return _columnsNames.length;
    }

    public int getRowCount() {
        return _listActions.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Object result = null;
        switch (columnIndex) {
            case 0:
                result = _listActions.get(rowIndex).getCategorie();
                if (result == null){
                    result = "Autre";
                }
                break;
            case 1:
                result = ChocoDate.formatDateTime(_listActions.get(rowIndex).getDate());
                break;
            case 2:
                result = _listActions.get(rowIndex).getDescription();
                break;
            case 3:
                result = _listActions.get(rowIndex).getAmount();
                break;
        }
        return result;
    }

    public void updateWithMonth(Month p_month){
        _listActions = p_month.getActions();
        fireTableDataChanged();
    }
}
